#import "ColorNameGame.h"

@interface ColorNameGame ()

@property NSInteger* currentColour;
@property NSInteger* currentText;
@property BOOL isGameOver;
@property NSArray* colors;

@end

@implementation ColorNameGame

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setName:@"ColorNameGame"];
        [self setType:reaction];
        self.ruleOfTheGame=@"Hit when color and text match";
        self.colors=[[NSArray alloc]initWithObjects:@"blue",@"white",@"red",@"yellow",@"orange",@"purple",@"green", nil];
        
    }
    return self;
}

-(void)draw{
    [self removeAllComponents];
    int text=arc4random_uniform(kColorNumber);
    int colour=arc4random_uniform(kColorNumber);
    
    NSString* labelText=[self.colors objectAtIndex:text];
    NSString* labelColor=[NSString stringWithFormat:@"%@Color",[self.colors objectAtIndex:colour]];
    
    
    self.currentColour=colour;
    self.currentText=text;
    
    LabelGameComponent* label=[[LabelGameComponent alloc]initWithText:labelText andFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight/kHeighDel) andFontSize:kScreenWidth/kWidthDel andColour:labelColor];
    LabelGameComponent* labelOne=[[LabelGameComponent alloc]initWithText:labelText andFrame:CGRectMake(0, kScreenHeight-kScreenHeight/kHeighDel - 2*kButtonHeigh, kScreenWidth, kScreenHeight/kHeighDel) andFontSize:kScreenWidth/kWidthDel andColour:labelColor];

        [self addComponents:label];
        [self addComponents:labelOne];
    
}

-(BOOL)isRight{
    if (self.currentText == self.currentColour) {
        return YES;
        
    }
    else{
        return NO;
    }
}

-(void)play{
    [self draw];
    
    [NSTimer scheduledTimerWithTimeInterval: 1                                                  target: self selector:@selector(newColor:)userInfo: nil repeats:YES];

}

-(void)newColor:(NSTimer*)timer{
    if (self.isGameOver==YES) {
        [timer invalidate];
    }
    else{
        [self draw];
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"SomeGames"
         object:self userInfo:nil];
        
    }

}

-(void)gameOver{
    self.isGameOver=YES;
}

@end
